package cn.buddha.jesw.mvc.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * @ClassName MyService
 * @Author 友野浩二
 * @Date 2019/12/4 20:29
 * @Description 异步service类: 使用 @Async 注解
 * @Version 1.0
 */
@Service
@Async
@Slf4j
public class MyService {

    /**
     * 下面是2个异步任务（不带返回值的）
     **/
    public void task1() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("Thread exception: ", e);
        }
    }

    public void task2() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            log.error("Thread exception: ", e);
        }
    }

    /**
     * 下面是2个异步任务（带返回值的，可以在调用出取地返回值）
     **/
    public Future<String> task5() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("Thread exception: ", e);
        }
        return new AsyncResult<>("task5");
    }

    public Future<String> task6() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            log.error("Thread exception: ", e);
        }
        return new AsyncResult<>("task6");
    }

}
